package com.baidu.shoppingmall.auth.service.impl;

import com.baidu.shoppingmall.auth.dao.CommentDAO;
import com.baidu.shoppingmall.auth.domain.CommentDTO;
import com.baidu.shoppingmall.auth.domain.CommentPO;
import com.baidu.shoppingmall.auth.domain.CommentVO;
import com.baidu.shoppingmall.auth.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentDAO, CommentPO> implements CommentService {

    private static final Logger LOGGER= LoggerFactory.getLogger(CommentServiceImpl.class);

    @Autowired
    private CommentDAO commentDAO;
    /**
     * 获取所有信息
     * @return
     */
    @Override
    public List<CommentDTO> getAll() {
        List<CommentDTO> commentDTOList=new ArrayList<>();
        try {
            List<CommentPO> commentPOList = commentDAO.selectList(null);
            commentPOList.forEach(po->commentDTOList.add(po.clone(CommentDTO.class)));
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("error", e);
        }
        return commentDTOList;
    }

    @Override
    public Integer insertNewComment(CommentDTO commentDTO) {
        try {
            CommentPO commentPO = commentDTO.clone(CommentPO.class);
            commentDAO.insert(commentPO);
        } catch (Exception e) {
           LOGGER.error("error", e);
           return 0;
        }
        return 1;
    }
}
